import store from '@/store'

const checkPermission = (el, binding) => {
  const { value: code } = binding;
  //修复商城页面复制之后serviceType获取到的是（？）
  const hrefRight = window.location.href.split("#/")[1];
  const pathArr = hrefRight.split("/");
  const serviceType =pathArr.length
    ? pathArr[0].toLocaleUpperCase()
    : store.state.user.currentService;
  const btnList = localStorage.getItem(serviceType + "Btn") ? JSON.parse(localStorage.getItem(serviceType + "Btn")) : [];

  // if (code.includes('140401')) {
  //   debugger
  // }

  if (!code || !(code instanceof Array)) {
    console.warn("传递给v-has的permisson 格式不正确 => " + code)
    return
  }

  if (!code.length) {
    console.warn("传递给v-has的permisson 数组为空 => " + code)
    return
  }

  if (!btnList) {
    console.warn("缓存的按钮权限数据获取异常 => " + btnList)
    return
  }

  const hasPermission = btnList.some((role) => code.includes(role));
  if (!hasPermission) {
    el.parentNode && el.parentNode.removeChild(el);
  }
};

export default {
  //按钮权限
  mounted(el, binding) {
    checkPermission(el, binding);
  },
  updated(el, binding) {
    checkPermission(el, binding);
  },
};
